package com.cjgl.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cjgl.common.BaseResponse;
import com.cjgl.common.ErrorCode;
import com.cjgl.common.ResultUtils;
import com.cjgl.exception.BusinessException;
import com.cjgl.mapper.TblAdminMapper;
import com.cjgl.model.dto.clazz.SelectByClazzNoOrClazzNameOrYearResquest;
import com.cjgl.model.entity.TblClass;
import com.cjgl.model.entity.TblStudents;
import com.cjgl.service.TblClassService;
import com.cjgl.service.TblStudentsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhangyuwu
 */
@RestController
@RequestMapping("/adminManageClassInfos")
public class AdminManageClassInfo {

	@Resource
	TblClassService classService;
	@Resource
	TblStudentsService tblStudentsService;
	@Resource
	TblAdminMapper tblAdminMapper;

	/**
	 * 查询班级信息 classNo className year(只精确到年) 查询
	 *
	 * @param clazzInfo
	 * @param request
	 * @return
	 */
	@PostMapping("/get/allClazz")
	public BaseResponse<Object> getClazzInfo(@RequestBody SelectByClazzNoOrClazzNameOrYearResquest clazzInfo, HttpServletRequest request) {

		String classNo = clazzInfo.getClassNo() == null ? "" : clazzInfo.getClassNo();
		String className = clazzInfo.getClassName() == null ? "" : clazzInfo.getClassName();
		String year = clazzInfo.getYear() == null ? "" : clazzInfo.getYear();


		LambdaQueryWrapper<TblClass> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper
				.like(!StringUtils.isEmpty(classNo), TblClass::getClassNo, classNo)
				.like(!StringUtils.isEmpty(className), TblClass::getClassName, className)
				.like(!StringUtils.isEmpty(year), TblClass::getClassDate, year);

		List<TblClass> tblClasses = classService.getBaseMapper().selectList(queryWrapper);

		return ResultUtils.success(tblClasses);
	}

	@GetMapping("/get/classMajor")
	public BaseResponse<Object> getClazzInfoClassMajor(HttpServletRequest request) {

		List<String> classMajor = tblAdminMapper.getClassMajor();
		return ResultUtils.success(classMajor);
	}

	/**
	 * 添加班级
	 * @param tblClass
	 * @param request
	 * @return
	 */
	@PostMapping("/add/clazz")
	public BaseResponse<Object> addClazz(@RequestBody TblClass tblClass,HttpServletRequest request) {
		if (tblClass==null){
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		boolean save = classService.save(tblClass);

		return ResultUtils.success(save);
	}

	/**
	 * 修改班级
	 * @param tblClass
	 * @param request
	 * @return
	 */
	@PostMapping("/update/clazz")
	public BaseResponse<Object> updateClazz(@RequestBody TblClass tblClass,HttpServletRequest request) {
		if (tblClass==null){
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		boolean save = classService.updateById(tblClass);

		return ResultUtils.success(save);
	}
	/**
	 * 修改班级
	 * @param tblClass
	 * @param request
	 * @return
	 */
	@Transactional
	@PostMapping("/delete/clazz")
	public BaseResponse<Object> deleteClazz(@RequestBody TblClass tblClass,HttpServletRequest request) {
		if (tblClass==null){
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		String classNo = tblClass.getClassNo();
		Integer integer = tblAdminMapper.updateStudent(classNo);
		boolean result = classService.removeById(tblClass);
		if (!result){
			throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
		}
		return ResultUtils.success(result);
	}

}
